package com.example.tests;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import com.example.utils.DateGenerator;
import com.thoughtworks.xstream.XStream;

public class ContactDataGenerator extends GeneratorBase{

	public static void main(String[] args) throws IOException {
		if(args.length < 3){
			System.out.println("Please specify parameter <amount of test data> <file> <format>");
			return;
		}
		
		int amount = Integer.parseInt(args[0]);
		File file = new File(args[1]);
		String format = args[2];
		
		if(file.exists()){
			System.out.println("File exists, please remote it manually" + file);
			return;
		}
		
		List<ContactData> groups = generateRandomContacts(amount);
		if("csv".equals(format)){
			saveContactsToCsvFile(groups, file);
		} else if("xml".equals(format)){
			saveContactsToXmlFile(groups, file);
		} else{
			System.out.println("Unknown format " + format);
			return;
		}
	}
	private static void saveContactsToXmlFile(List<ContactData> contacts, File file) throws IOException {
		XStream xstream = new XStream();
		xstream.alias("contact", ContactData.class);
		String xml = xstream.toXML(contacts);
		FileWriter writer = new FileWriter(file);
		writer.write(xml);
		writer.close();
		
	}

	public static List<ContactData> loadContactsFromXmlFile(File file) {
		XStream xstream = new XStream();
		xstream.alias("contact", ContactData.class);
		return (List<ContactData>) xstream.fromXML(file);
	}
	
	private static void saveContactsToCsvFile(List<ContactData> contacts, File file) throws IOException {
		FileWriter writer = new FileWriter(file);
		for (ContactData contact : contacts) {
			writer.write(contact.getFirstName() + "," + 
		        contact.getLastName() + "," +
			    contact.getFirstAddress() + "," +
			    contact.getHomePhone() + "," +
			    contact.getMobilePhone() + "," +
			    contact.getWorkPhone() + "," +
			    contact.getFirstEmail() + "," +
			    contact.getSecondEmail() + "," +
			    contact.getBDay() + "," +
			    contact.getBMonth() + "," +
			    contact.getBYear() + "," +
			    contact.getGroup() + "," +
			    contact.getSecondAdderess() + "," +
			    contact.getPhone() + ",|" + "\n");
		}
		writer.close();
	}

	public static List<ContactData> generateRandomContacts(int amount) {
        List<ContactData> list = new ArrayList<ContactData>();		
        for (int i = 0; i < amount; i++) {
			DateGenerator dateData = new DateGenerator();
			ContactData contact = new ContactData()
				.withFirstName(generateRandomString())
				.withLastName(generateRandomString())
				.withFirstAddress(generateRandomString())
				.withHomePhone(generatePhone(dateData))
				.withMobilePhone(generatePhone(dateData))
				.withWorkPhone(generatePhone(dateData))
				.withFirstEmail(generateEmailAddress())
				.withSecondEmail(generateEmailAddress())			
				.withBDay(Integer.toString(dateData.getDay()))
				.withBMonth(dateData.getMonth())
				.withBYear(Integer.toString(dateData.getYear()))
				.withGroup(generateRandomString())
				.withSecondAdderess(generateRandomString())
				.withPhone(generatePhone(dateData));
			list.add(contact);
		}
		return list;
	}
	
	public static List<ContactData> loadContactsFromCsvFile(File file) throws IOException {
		List<ContactData> list = new ArrayList<ContactData>();
		FileReader reader = new FileReader(file);
		BufferedReader bufferedReader = new BufferedReader(reader);
		String line = bufferedReader.readLine();
		while (line != null) {
			String[] part = line.split(",");
			ContactData contact = new ContactData()
			.withFirstName(part[0])
			.withLastName(part[1])
			.withFirstAddress(part[2])
			.withHomePhone(part[3])
			.withMobilePhone(part[4])
			.withWorkPhone(part[5])
			.withFirstEmail(part[6])
			.withSecondEmail(part[7])			
			.withBDay(part[8])
			.withBMonth(part[9])
			.withBYear(part[10])
			.withGroup(part[11])
			.withSecondAdderess(part[12])
			.withPhone(part[13]);
			list.add(contact);
			line = bufferedReader.readLine();
		}
		bufferedReader.close();
		return list;
	}	
}
